package com.lanswon.qzsmk.util.inventoryUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;

import org.springframework.stereotype.Component;

import ch.qos.logback.classic.Logger;

@Component
public class ListTurnToSet {
	public List<String> listToSet(List<String> list){
		HashSet<String> listToSet = new HashSet<String>(list);
		list.clear();
		list.addAll(listToSet);
		return list;
	}
	
	/**
	 * 将所有的卡号进行排序，并且整理出卡号段
	 * @param totalCitizenCardNoList
	 * @return
	 */
	public List<String[]> modifyCitizenNoList(List<String> totalCitizenCardNoList) {
		List<String[]> segementUsedList = new ArrayList<String[]>();
		Collections.sort(totalCitizenCardNoList,new Comparator<String>()
		{	
			@Override
			public int compare(String o1, String o2) {
				int compare1 = Integer.parseInt(o1.substring(5));
		        int compare2 = Integer.parseInt(o2.substring(5));
		        return compare1 - compare2;
			}

		});
		String startSegmentString = totalCitizenCardNoList.get(0).substring(5);
		Integer startSegment = Integer.parseInt(startSegmentString);
		String previousString;
		Integer previous;
		for(int i=1;i<totalCitizenCardNoList.size();i++){
			String curString = totalCitizenCardNoList.get(i).substring(5);
			Integer cur = Integer.parseInt(totalCitizenCardNoList.get(i).substring(5));
			previousString = totalCitizenCardNoList.get(i-1).substring(5);
			previous = Integer.parseInt(previousString);
			if((cur-previous)>1){
				String[] segmentArray = new String[2];
				segmentArray[0]=startSegmentString;
				segmentArray[1]=previousString;
				segementUsedList.add(segmentArray);
				if(i==totalCitizenCardNoList.size()-1){
					segementUsedList.add(new String[]{curString,curString});
				}else if(i<(totalCitizenCardNoList.size()-1)){
					startSegmentString = curString;
				}
			}
			if((cur-previous)==1&&(i==totalCitizenCardNoList.size()-1)){
				segementUsedList.add(new String[]{startSegmentString,curString});
			}
			
		}
		return segementUsedList;
	}
}
